package com.ryskoo.customer.vendorinfo.repository;

import com.ryskoo.common.constant.CacheConstant;
import com.ryskoo.customer.vendorinfo.domain.VendorInfoPO;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author hh
 */
@Primary
@Repository
public class VendorInfoRedisImpl extends BasicRedisImpl<VendorInfoPO> implements IVendorInfoRepository {

	@Autowired
	private RedisUtil redisClient;

	@Autowired
	private IVendorInfoRepository VendorInfoRepository;

	@PostConstruct
	public void init() {
		this.setRedisClient(redisClient);
		this.setDao(VendorInfoRepository);
	}

	public VendorInfoRedisImpl() {
		super(CacheConstant.CUSTOMER_VENDOR_INFO_KEY);
	}

	@Override
	public void create(VendorInfoPO m) {
		super.create(m);
		redisClient.setObject(CacheConstant.CUSTOMER_VENDOR_INFO_KEY + m.getVendorUuid(), m);
	}

	@Override
	public void update(VendorInfoPO m) {
		super.update(m);
		redisClient.setObject(CacheConstant.CUSTOMER_VENDOR_INFO_KEY + m.getVendorUuid(), m);
	}

	@Override
	public void delete(VendorInfoPO m) {
		super.delete(m);
		redisClient.remove(CacheConstant.CUSTOMER_VENDOR_INFO_KEY + m.getVendorUuid());
	}

	@Override
	public void createOrUpdate(VendorInfoPO m) {
		super.createOrUpdate(m);
		redisClient.setObject(CacheConstant.CUSTOMER_VENDOR_INFO_KEY + m.getVendorUuid(), m);
	}

	/**
	 * 根据VendorUuids 查询 VendorInfo
	 * 
	 * @param VendorUuids
	 * @return
	 */
	public List<VendorInfoPO> getByVendorUuids(List<String> vendorUuids) {
		return VendorInfoRepository.getByVendorUuids(vendorUuids);
	}

	/**
	 * 根据 VendorUuid 查询 vendorInfo
	 * 
	 * @param VendorUuid
	 * @return
	 */
	public VendorInfoPO getByVendorUuid(String vendorUuid) {
		Object obj = redisClient.getObject(CacheConstant.CUSTOMER_VENDOR_INFO_KEY + vendorUuid, VendorInfoPO.class);
		if (null != obj) {
			return (VendorInfoPO) obj;
		}
		VendorInfoPO m = VendorInfoRepository.getByVendorUuid(vendorUuid);
		if (null != m) {
			redisClient.setObject(CacheConstant.CUSTOMER_VENDOR_INFO_KEY + m.getVendorUuid(), m);
		}
		return m;
	}

	/**
	 * 根据 VendorUuids 删除 VendorInfo
	 * 
	 * @param List<String>
	 *            needDeleteVendorUuids
	 * @return
	 */
	public void deletesByVendorUuids(List<String> needDeleteVendorUuids) {
		VendorInfoRepository.deletesByVendorUuids(needDeleteVendorUuids);
		// 删除缓存
		if (needDeleteVendorUuids != null && needDeleteVendorUuids.size() > 0) {
			for (String vendorUuid : needDeleteVendorUuids) {
				redisClient.remove(CacheConstant.CUSTOMER_VENDOR_INFO_KEY + vendorUuid);
			}
		}
	}
}
